package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import controle.ArduinoIntervalController;
import controleBD.UsuarioEventoDAO;

@WebServlet("/NovoEventoServlet")
public class NovoEventoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public NovoEventoServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		if(request.getParameter("button").equals("OK")){
			
			UsuarioEventoDAO eventAdd = new UsuarioEventoDAO();
		
			eventAdd.adicionaEvento(request.getSession().getAttribute("username").toString(), request.getParameter("evento"), request.getParameter("min"), request.getParameter("max"));
		
			if (request.getParameter("evento").equals("Luminosidade")){
				ArduinoIntervalController.adicionaValoresLuz(Integer.parseInt(request.getParameter("min")), Integer.parseInt(request.getParameter("max")));
			}else if(request.getParameter("evento").equals("Temperatura")){
				ArduinoIntervalController.adicionaValoresTemp(Integer.parseInt(request.getParameter("min")), Integer.parseInt(request.getParameter("max")));
			}
				
			
			
		}
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.print("<body onLoad=\"window.close()\"> </body>");
	
		
	}

}
